<?php
namespace app\agent\controller;

use app\common\controller\AgentAction;
use think\Db;

class OrderOperate extends AgentAction
{
	protected $modelClass = '\app\common\model\Orders';

	public function initialize()
    {
        parent::initialize();
    }

	/**
     * @Title: cancelOrder
     * @Description: todo(取消未支付的订单)
     * @param ordersn 订单号
     * @return json
     * @author 若谷
     * @date 2019年6月4日
     * @throws
     */
    public function cancelOrder(){
        if (request()->isPost()) {
            $post = input("post.");
            $order = $this->cModel->where("ordersn",$post['id'])->where("status",1)->where("uid",UID)->find();
            if ($order) {
                $data = [
                    'id' => $order['id'],
                    'status' => 0,
                    'cancel_time' => time(),
                ];
                $result = $order->allowField(true)->save($data);
                if ($result) {
                	$url = $post['index'];
                    return ajax_return('取消成功',url('Order/'.$url));
                }else{
                	return ajax_return($this->cModel->getError());
                }
            }
        }
        return ajax_return('操作失败');
    }

    /**
     * @Title: delete
     * @Description: todo(删除已关闭的订单)
     * @param id 订单id
     * @return json
     * @author 若谷
     * @date 2019年6月4日
     * @throws
     */
    public function delete()
    {
        if (request()->isPost()){
            $id = input('id');
            if (isset($id) && !empty($id)){
                $id_arr = explode(',', $id);
                $where[] = ['id', 'in', $id_arr];
                $where[] = ['uid','=',UID];
                $result = $this->cModel->where($where)->delete();
                if ($result){
                	$url = input("index");
                    return ajax_return('删除成功',url('Order/'.$url));
                }else{
                    return ajax_return($this->cModel->getError());
                }
            }
        }
    }

    /**
     * @Title: refundOrder
     * @Description: todo(订单退款)
     * @param ordersn 订单号
     * @return json
     * @author 若谷
     * @date 2019年6月4日
     * @throws
     */
    public function refundOrder(){
    	
    }

    /**
     * @Title: appointment
     * @Description: todo(预约拍摄)
     * @param ordersn 订单号
     * @param index 来源
     * @return json
     * @author 若谷
     * @date 2019年6月5日
     * @throws
     */
    public function appointment($ordersn,$index=''){
        if (request()->isPost()){
            $post = input('post.');
            $where[] = [
                ['ordersn','=',$post['ordersn']],
                ['uid','=',UID],
            ];
            $order = $this->cModel->where($where)->find();
            if ($order) {
                $data = [
                    'id' => $order['id'],
                    'status' => 3,
                    'shoot_time' => strtotime($post['shoot_time']),
                    'appointment_time' => time(),
                ];
                $result = $this->cModel->allowField(true)->save($data, $data['id']);
                if ($result){
                    return ajax_return(lang('action_success'), url('order/'.$index));
                }else{
                    return ajax_return($this->cModel->getError());
                }
            }
        }else{
            $where[] = [
                ['ordersn','=',$ordersn],
                ['uid','=',UID],
            ];
            $data = $this->cModel->where($where)->find();
            $this->assign('data', $data);
            return $this->fetch();
        }
    }

    /**
     * @Title: uploadimgs
     * @Description: todo(摄影师上传图片)
     * @param ordersn 订单号
     * @return json
     * @author 若谷
     * @date 2019年6月5日
     * @throws
     */
    public function uploadimgs($ordersn,$index=''){
        if (request()->isPost()){
            $data = input('post.');
            $where[] = [
                ['ordersn','=',$data['ordersn']],
                ['uid','=',UID],
            ];
            $order = $this->cModel->where($where)->find();
            if ($order) {
                $data['mid'] = $order['mid'];
                $data['order_id'] = $order['id'];
                $data['images'] = json_encode($data['images']);
                $result = $this->validate($data, 'app\common\validate\OrdersPhotos.add');
                if(true !== $result){
                    return ajax_return($result);
                }
                $model = new \app\common\model\OrdersPhotos;
                $status = [
                    'id' => $order['id'],
                    'status' => 5,
                    'photos_time' => time(),
                ];
                $result = 0;
                Db::startTrans();
                try{
                    if ($order->photos) {
                        $data['id'] = $order->photos->id;
                        $model->allowField(true)->save($data,$data['id']);
                    }else{
                        $model->allowField(true)->save($data);
                    }
                    $this->cModel->allowField(true)->save($status,$status['id']);
                    Db::commit();
                    $result = 1;
                } catch (\Exception $e) {
                    Db::rollback();
                }
                if ($result){
                    return ajax_return(lang('action_success'), url('order/'.$index));
                }else{
                    return ajax_return($model->getError());
                }
            }
        }else{
            $where[] = [
                ['ordersn','=',$ordersn],
                ['uid','=',UID],
            ];
            $order = $this->cModel->where($where)->find();
            if ($order->photos) {
                $data = $order->photos;
                $data->images = json_decode($data->images,1);
                $data = json_encode($data);
                $data = json_decode($data,1);
            }
            $data['ordersn'] = $ordersn;
            $this->assign("data",$data);
            return $this->fetch();
        }
    }

    /**
     * @Title: chooseimg
     * @Description: todo(选片)
     * @param ordersn 订单号
     * @return json
     * @author 若谷
     * @date 2019年6月5日
     * @throws
     */
    public function chooseimg($ordersn,$index=''){

    }

}